﻿using System.Data;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace Koala.Pro.Helpers
{
    public static class TextSharpHelper
    {
        public static bool ExportTableToPdf(string title, DataTable data, string pdfFile, bool isLandscape = true, bool includeHeader = true, HorizontalAlignment headerAlignment = HorizontalAlignment.CENTER, float headerFontSize = 9f, float rowFontSize = 9f, float? headerFixHeight = null)
        {
            PdfFontFactory.Register("C:\\WINDOWS\\Fonts\\simsun.ttc");
            PdfWriter pdfWriter = new PdfWriter(pdfFile);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            pdfDocument.SetDefaultPageSize(isLandscape ? PageSize.A4.Rotate() : PageSize.A4);
            Document document = new Document(pdfDocument);
            PdfFont font = PdfFontFactory.CreateRegisteredFont("simsun", "Identity-H", PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED, cached: true);
            document.SetFont(font).SetFontSize(12f);
            if (!string.IsNullOrEmpty(title))
            {
                Paragraph element = new Paragraph(title).SetFontColor(ColorConstants.BLACK).SetBold().SetFontSize(headerFontSize + 5f)
                    .SetTextAlignment(TextAlignment.CENTER);
                document.Add(element);
            }
            Table table = new Table(data.Columns.Count).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetWidth(new UnitValue(2, 100f));
            table.UseAllAvailableWidth();
            foreach (DataColumn column in data.Columns)
            {
                string text = ((!string.IsNullOrEmpty(column.Caption)) ? column.Caption : column.ColumnName);
                Cell cell = new Cell().Add(new Paragraph(text)).SetBold().SetVerticalAlignment(VerticalAlignment.MIDDLE)
                    .SetHorizontalAlignment(headerAlignment)
                    .SetPadding(1f)
                    .SetFontSize(headerFontSize);
                if (headerFixHeight.HasValue)
                {
                    cell.SetHeight(new UnitValue(1, headerFixHeight.Value));
                }
                table.AddHeaderCell(cell);
            }
            DeviceCmyk deviceCmyk = Color.ConvertRgbToCmyk(WebColors.GetRGBColor("White"));
            DeviceCmyk deviceCmyk2 = Color.ConvertRgbToCmyk(WebColors.GetRGBColor("LightCyan"));
            for (int i = 0; i < data.Rows.Count; i++)
            {
                table.StartNewRow();
                DeviceCmyk backgroundColor = ((i % 2 == 0) ? deviceCmyk : deviceCmyk2);
                for (int j = 0; j < data.Columns.Count; j++)
                {
                    string text2 = data.Rows[i][j].ToString();
                    Cell cell2 = new Cell().SetBackgroundColor(backgroundColor).SetFontSize(rowFontSize).SetVerticalAlignment(VerticalAlignment.MIDDLE)
                        .Add(new Paragraph(text2));
                    table.AddCell(cell2);
                }
            }
            document.Add(table);
            pdfDocument.Close();
            ((Stream)(object)pdfWriter).Close();
            return true;
        }
    }
}
